package com.cmpan.networkdisk.interceptor;

import com.cmpan.networkdisk.model.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName:MyInterceptor
 * Package:com.cmpan.networkdisk.interceptor
 * Description:
 *
 * @Date:2020/7/6 21:02
 * @com.chuangmei
 */

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("进入拦截器...");
        //拿到user
        User user = (User) request.getSession().getAttribute("user");
        System.out.println("user = " + user);

        //如果user为null,那么跳转至登录界面
        if (user == null){
            response.sendRedirect("/loginIndex");
        }

        return user == null ? false : true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
